/**
 * 绑定事件
 */
function bindEvent() {

    //给查询按钮绑定点击事件
    $("#search").click(function () {

        //点击查询后，保存此时查询区的值
        status = $.trim($("#zt").val());
        caseNum = $.trim($("#ah").val());
        executor = $.trim($("#mc").val());
        idNum = $.trim($("#zjhm").val());

        //根据点击后输入框的值进行查询
        loadData(status, caseNum, executor, idNum);
    });
}

/**
 * 根据查询条件加载数据
 * @param zt 状态
 * @param ah 案号
 * @param mc 名称
 * @param zjhm 证件号码
 */
function loadData(zt, ah, mc, zjhm) {
    mygrid.clearAll();

    mygrid.loadXML(CONTEXT_PATH + "ck/queryCkdx?zt=" + zt + "&ah=" + ah
        + "&mc=" + mc + "&zjhm=" + zjhm);
}

/**
 * 初始化表格
 */
function initMyGrid() {
    mygrid = new dhtmlXGridObject('grid');
    mygrid.setImagePath(CONTEXT_PATH + "resources/v2/static/plugg/dhtmlx/dhtmlxGrid/codebase/imgs/");
    mygrid.setHeader("<input type='checkbox' onclick='checks()'/>,状态,查看,编辑,案号,被执行人,证件类型,证件号码,查控范围,承办人,书记员");

    //设置列宽 列类型 列排序依据 列显示位置
    mygrid.setInitWidthsP("6,8,6,6,10,13,8,*,15,8,8");
    mygrid.setColTypes("ch,ro,img,img,ro,ro,ro,ro,ro,ro,ro");
    mygrid.setColSorting("na,str,na,na,str,str,str,str,str,str,str");
    mygrid.setColAlign("center,center,center,center,left,left,center,left,center,center,center");

    //设置皮肤
    mygrid.setSkin("tdh_default");

    //设置分页排版 每页条数的选择项 默认10条
    mygrid.pagingLayout("first,prev,page,next,last,limit,refresh,skip,count");
    mygrid.pagingLimits("10,20,50");
    mygrid.enablePagingon(true, 10);

    //初始化
    mygrid.init();
}

/**
 * 查询状态下拉框
 * @param zt 状态
 */
function queryStatus(zt) {
    $.ajax({
        type: "get",
        url: CONTEXT_PATH + "ck/queryZt",
        dataType: "json",
        contentType: 'application/json',
        success: function (result) {
            if (result.code === 200) {

                //获取查询结果
                var tsdm = result.data;
                var html = '';

                //插入下拉框选项
                for (var i = 0; i < tsdm.length; i++) {
                    html += "<option value='" + tsdm[i].code + "'>" + tsdm[i].mc + "</option>"
                }
                zt.html(html);

                //重新渲染下拉
                selectInit(zt);
            } else {
                layer.msg(result.message, {time: 1000});
            }
        },
        error: function () {
            layer.alert("状态下拉框数据加载异常！");
        }
    });
}



/**
 * 根据id批量删除查控对象
 * @param ids id列表
 */
function deleteUser(ids) {

    if (ids.length <= 0) {

        layer.msg("未选择登记状态的记录，无法删除", {time: 1000});
    } else {

        //获取id数组
        let idArr = ids.split(",");

        //存储状态为登录的id
        let deleteIds = [];
        for(let i = 0; i < idArr.length; i++) {
            let id = idArr[i];
            let zt = mygrid.cells(id, 1).getValue();
            if (zt === '登记') {
                deleteIds.push(id);
            }
        }

        ids = JSON.stringify(deleteIds);

        let layerMsg = '存在非登记状态的记录，只会删除登记状态的记录，是否继续';
        if (deleteIds.length === idArr.length) {
            layerMsg = '是否删除选中的记录';
        }
        layer.confirm(layerMsg, {
            btn: ['确定', '取消'],
            title: "删除"
        }, function () {
            $.ajax({
                type: "POST",
                url: CONTEXT_PATH + "ck/deleteCkdxes",
                contentType: 'application/json;charset=utf-8',
                data: ids,
                success: function (result) {

                    // 删除成功
                    if (result.code === 200) {

                        //刷新表格
                        loadData(status, caseNum, executor, idNum);

                        layer.msg("删除成功！", {time: 1000});
                    } else {
                        layer.msg("删除失败！", {time: 1000});
                    }
                },
                error: function (r, s, e) {
                    layer.alert("请求异常！");
                }
            });
        });
    }
}

/**
 * 新增
 */
function addUser() {
    layer.open({
        type: 2,
        title: '新增查询申请',
        area: ['800px', '640px'],
        shadeClose: false,      //点击遮罩不关闭层
        content: CONTEXT_PATH + 'ck/showApplicationPage?type=add&id='
    })
}

/**
 * 打开查看详细用户信息窗口
 * @param id 用户id
 */
function view(id) {
    layer.open({
        type: 2,
        title: '查看信息',
        area: ['800px', '640px'],
        shadeClose: false,      //点击遮罩不关闭层
        content: CONTEXT_PATH + 'ck/showApplicationPage?type=view&id=' + encodeURI(id)
    })
}

/**
 * 打开修改详细用户信息窗口
 * @param id 用户id
 */
function edit(id) {
    layer.open({
        type: 2,
        title: '编辑信息',
        area: ['800px', '640px'],
        shadeClose: true,
        content: CONTEXT_PATH + 'ck/showApplicationPage?type=edit&id=' + encodeURI(id)
    })
}

/**
 * 设置表中所有复选框选中状态
 */
function checks() {
    if (checkedStatus === true) {
        mygrid.checkAll(false);
        checkedStatus = false;
    } else {
        mygrid.checkAll(true);
        checkedStatus = true;
    }
}
